import java.io.*;
import java.lang.*;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class assignmentHandle
{
	//private static DBConnection conn = new DBConnection(); 
	//private static DBActions runSQL = new DBActions(conn);

 	public static void main(String[] args)
	{
		
		//runSQL.Select("select * from Assignment");
		HandleInput input= new HandleInput();
		input.HandleUserInput();
		
		
	}   
}

//handle input parameters and get the marking scheme
class HandleInput{

	//public class HandleInput {
        
    /**
     * Creates a new instance of <code>HandleInput</code>.
     */
    private int AssignmentID;
    private String AssignmentName;
    private int MaximumMark;
    private String Status;
    private String DueDate;
    private String sqlString;
    
    private static DBConnection conn = new DBConnection();
    private static DBActions runSQL = new DBActions(conn);
	
    public void HandleUserInput() {
    	try{
    		
    		BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
    		dataValidation isValid= new dataValidation();
    		
    		while (System.in.available() > 0) //check enter
                	System.in.read(); //flush the buffer
                
            	String choice="nothing";
            	int isFound=0;
                while(!choice.equalsIgnoreCase("e")){
                	System.out.println("What do you want to do?");
                	System.out.println("Enter 'c' to create new Assignment.");
                	System.out.println("Enter 'm' to modify status of the assignment.");
                	System.out.println("Enter 'd' to modify status of the assignment.");
                	System.out.println("Enter 'r' to list out assignments");
                	System.out.println("Enter 'e' To exit");	
                	System.out.print("You choose: ");
                	choice=inFromUser.readLine();
                	System.out.println();
                	
                	//create new assignment
                	if(choice.equalsIgnoreCase("c"))
                	{
                		System.out.print("Enter AssignmentID: ");
                		String str=inFromUser.readLine();
						
						if(isValid.validateInt(str)==0)
						{
							AssignmentID=Integer.parseInt(str);
							
							//check primary key here
							sqlString="select AssignmentID from Assignment where AssignmentID="+AssignmentID;
							isFound=runSQL.searchSQL(sqlString);
						
							if(isFound==0){
								System.out.print("Enter Assignment Name: ");
								AssignmentName=inFromUser.readLine();
								
								System.out.print("Enter Maximum Mark: ");
								MaximumMark=Integer.parseInt(inFromUser.readLine());
								
								System.out.print("Enter Status: ");
								Status=inFromUser.readLine();
								
								System.out.print("Enter due date: ");
								DueDate=inFromUser.readLine();
								
								if(isValid.validateString(AssignmentName)==0 
										|| isValid.validateString(Status)==0 || 
											isValid.validateString(DueDate)==0)
								{
										sqlString="insert into Assignment values ("+AssignmentID+",'"+AssignmentName+"',"
																		+MaximumMark+",'"+Status+"','"+DueDate+"')";
								
										runSQL.Insert(sqlString);
										System.out.println();
										System.out.println("Inserted!");
										System.out.println();
								}
								else
								{
									System.out.println();
									System.out.println("invalid character!");
									System.out.println();
									System.out.println();									
								}
								
							}
							else
							{
								System.out.println();
								System.out.println("AssignmentID is existed!");
								System.out.println();
							}//end checking
						}
						else
							System.out.println("Invalid! AssignmentID must be a numberic.");
				
                	}//end of create assignment
                	
                	//modify existed assignment
                	else if (choice.equalsIgnoreCase("m")){
                		System.out.print("Enter AssignmentID: ");
						AssignmentID=Integer.parseInt(inFromUser.readLine());
						
						sqlString="select AssignmentID from Assignment where AssignmentID="+AssignmentID;
						
						//runSQL.Select("select * from Assignment");
						isFound=runSQL.searchSQL(sqlString);
						
						if(isFound!=0){
							System.out.print("Enter new status: ");
							Status=inFromUser.readLine();
							
							sqlString="update Assignment set Status='"+Status+"'where AssignmentID="+AssignmentID;
							runSQL.Update(sqlString);
							System.out.println();
							System.out.println("Updated!");
							System.out.println();
						}
						else
						{
							System.out.println();
							System.out.println("AssignmentID is not existed!");
							System.out.println();
							System.out.println();
						}
						
                	}//end of modify
                	
                	//delete assignment
                	else if(choice.equalsIgnoreCase("d"))
                	{
                		System.out.print("Enter AssignmentID: ");
                		String str=inFromUser.readLine();
							AssignmentID=Integer.parseInt(str);
							
							//check primary key here
							sqlString="select AssignmentID from Assignment where AssignmentID="+AssignmentID;
							isFound=runSQL.searchSQL(sqlString);
						
							if(isFound!=0){
								runSQL.Delete("delete from Assignment where AssignmentID="+AssignmentID);
								System.out.println();
								System.out.println("Deleted!");
								System.out.println();
							}
							else
								System.out.println(AssignmentID+" is not existed in Database");
                	}
                	
                	else if(choice.equalsIgnoreCase("r"))
                	{
                		System.out.println();
                		System.out.println();
                		sqlString="select * from Assignment";
                		runSQL.Select(sqlString);
                	}//end of list assignment
                }   
                
		}catch(IOException e){
			System.out.println(e);
            
		}catch(NumberFormatException e){
			System.out.println("Integer number is required!");
		}
    }//end of HandleUserInput method
}//end of HandleInput class


//validate data input
class dataValidation{
   
   public int validateString(String str){                        
      
      //Checks for validate string
      //inappropriate symbols like dots or @ signs or =.
      Pattern p = Pattern.compile("[a-zA-Z]|[0-9]|\\/");
      Matcher m = p.matcher(str);
      if (!m.find())
      {
      	return 1;
      }
      
      return 0;
   }   
      
   //Checks for valid numberic
   public int validateInt(String str)
   {
      
      Pattern p = Pattern.compile("[0-9]");
      Matcher m = p.matcher(str);
      
      if (!m.find())
      {
      	return 1;
      }
      return 0;
   }

}
